package com.example.clock.ChangeTips;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.example.clock.R;
import com.example.clock.basicClass.HelpFunc;
import com.example.clock.basicClass.Plan;
import com.example.clock.basicClass.Tags;
import com.example.clock.controlClass.DatabaseHelper;
import com.example.clock.controlClass.SqlController;
import com.example.clock.planPage.DetailPlan;
import com.example.clock.planPage.NewPlan;
import com.example.clock.planPage.PlanPage;

public class ChangeTips extends AppCompatActivity {
    String bigTags[];
    String smallTags[][];
    String selectedTags;
    View selectedView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_tips);

        //增加标签的button监听按钮
        Button button1 = findViewById(R.id.button21);
        Button button2 = findViewById(R.id.button22);

        button1.setOnClickListener(v -> {
            // 在按钮点击时执行的操作
            // 可以在这里添加您的逻辑
            AlertDialog.Builder builder = new AlertDialog.Builder(ChangeTips.this);
            builder.setTitle("新增大标签");

// 设置对话框布局
            LinearLayout layout = new LinearLayout(ChangeTips.this);
            layout.setOrientation(LinearLayout.VERTICAL);

// 设置EditText
            final EditText editText = new EditText(ChangeTips.this);
            layout.addView(editText);

            builder.setView(layout);

// 设置确定按钮
            builder.setPositiveButton("确定添加", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String newTag = editText.getText().toString();
                    // 添加新标签
                    SQLiteDatabase db = new DatabaseHelper(ChangeTips.this,ChangeTips.this).getWritableDatabase();
                    if(HelpFunc.checkForNullFormat(ChangeTips.this,newTag)){
                        SqlController.addBigTag(db,newTag);
                        ChangeTips.this.onStart();
                    }
                }
            });

// 设置取消按钮
            builder.setNegativeButton("取消添加", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // 在这里执行取消添加的逻辑，如果需要的话
                }
            });

// 创建并显示对话框
            builder.create().show();
        });
        button2.setOnClickListener(v -> {
            // 在按钮点击时执行的操作
            // 可以在这里添加您的逻辑
            AlertDialog.Builder builder = new AlertDialog.Builder(ChangeTips.this);
            builder.setTitle("新增小标签");

// 设置对话框布局
            LinearLayout layout = new LinearLayout(ChangeTips.this);
            layout.setOrientation(LinearLayout.VERTICAL);

// 设置EditText
            final EditText editText = new EditText(ChangeTips.this);
            layout.addView(editText);

            builder.setView(layout);

// 设置确定按钮
            builder.setPositiveButton("确定添加", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String newTag = editText.getText().toString();
                    // 添加新标签
                    SQLiteDatabase db = new DatabaseHelper(ChangeTips.this,ChangeTips.this).getWritableDatabase();
                    if(HelpFunc.checkForNullFormat(ChangeTips.this,newTag)){
                        SqlController.addSmallTag(db,selectedTags,newTag);
                        ChangeTips.this.onStart();
                    }
                }
            });

// 设置取消按钮
            builder.setNegativeButton("取消添加", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // 在这里执行取消添加的逻辑，如果需要的话
                }
            });

// 创建并显示对话框
            builder.create().show();
        });
    }
    @Override
    protected void onStart() {
        super.onStart();
        //加载数据
        SQLiteDatabase db = new DatabaseHelper(this,this).getWritableDatabase();
        bigTags = Tags.getStringBigTags(db);
        smallTags = Tags.getStringSmallTags(db);

        ListView lbig = (ListView) findViewById(R.id.bigtags);
        ListView lsmall = (ListView) findViewById(R.id.smalltags);
        selectedTags = bigTags[0];

        tipsAdapter adapter1 = new tipsAdapter(this,R.layout.list_item_tips,bigTags);
        lbig.setAdapter(adapter1);
        tipsAdapter adapter2 = new tipsAdapter(this,R.layout.list_item_tips, HelpFunc.deleteEmptyElementinArray(smallTags[0]));
        lsmall.setAdapter(adapter2);

        lbig.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // 等视图树渲染完成后再获取第一项的View
                View firstItemView = lbig.getChildAt(0);

                if (firstItemView != null) {
                    selectedView = firstItemView;
                    selectedView.setBackgroundColor(Color.GRAY);
                }

                // 移除监听器，以免多次调用
                lbig.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });



        //设定点击监听函数
        lbig.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 处理点击事件
                //修改选中项的背景颜色
                if (selectedView != null) {
                    selectedView.setBackgroundColor(Color.TRANSPARENT);
                }
                selectedView = view;
                // 记录当前选中项的View
                selectedView = view;
                // 修改当前选中项的背景颜色
                view.setBackgroundColor(Color.GRAY);


                selectedTags = (String)parent.getItemAtPosition(position);
                lsmall.setAdapter(new tipsAdapter(ChangeTips.this,R.layout.list_item_tips,HelpFunc.deleteEmptyElementinArray(smallTags[position])));
            }
        });
        lbig.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id){
                    String bigtips = (String) parent.getItemAtPosition(position);
                    Context context = ChangeTips.this;
                    //弹窗修改大标签
                    {
                        // 创建一个 AlertDialog.Builder 实例
                        AlertDialog.Builder builder = new AlertDialog.Builder(context);

                        // 获取布局填充器
                        LayoutInflater inflater = LayoutInflater.from(context);
                        View dialogView = inflater.inflate(R.layout.custom_dialog_layout, null);

                        // 获取布局中的 EditText
                        final EditText editText = dialogView.findViewById(R.id.editText);

                        // 设置对话框的视图
                        builder.setView(dialogView)
                                .setTitle(bigtips)
                                .setPositiveButton("确定修改", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // 用户点击确定修改按钮时的处理
                                        String userInput = editText.getText().toString();
                                        // 在这里处理用户输入的内容
                                        SQLiteDatabase db = new DatabaseHelper(context,context).getWritableDatabase();
                                        SqlController.changeBigTag(db,bigtips,userInput);
                                        onStart();
                                    }
                                })
                                .setNeutralButton("取消修改", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // 用户点击取消修改按钮时的处理
                                        dialog.dismiss();
                                    }
                                })
                                .setNegativeButton("删除标签",new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // 用户点击删除标签按钮时的处理
                                        SQLiteDatabase db = new DatabaseHelper(context,context).getWritableDatabase();
                                        SqlController.deleteBigTag(db,bigtips);
                                        onStart();
                                    }
                                });
                        // 创建并显示对话框
                        AlertDialog dialog = builder.create();
                        dialog.show();
                    }




                    return true;
                }
               });
        lsmall.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Context context = ChangeTips.this;
                // 处理点击事件，输入框输入
                String smalltip = (String) parent.getItemAtPosition(position);
                {
                    // 创建一个 AlertDialog.Builder 实例
                    AlertDialog.Builder builder = new AlertDialog.Builder(context);

                    // 获取布局填充器
                    LayoutInflater inflater = LayoutInflater.from(context);
                    View dialogView = inflater.inflate(R.layout.custom_dialog_layout, null);

                    // 获取布局中的 EditText
                    final EditText editText = dialogView.findViewById(R.id.editText);

                    // 设置对话框的视图
                    builder.setView(dialogView)
                            .setTitle(selectedTags+"-->"+smalltip)
                            .setPositiveButton("确定修改", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    // 用户点击确定修改按钮时的处理
                                    String userInput = editText.getText().toString();
                                    // 在这里处理用户输入的内容
                                    SQLiteDatabase db = new DatabaseHelper(context,context).getWritableDatabase();
                                    SqlController.deleteSmallTag(db,selectedTags,smalltip);
                                    SqlController.addSmallTag(db,selectedTags,userInput);
                                    onStart();
                                }
                            })
                            .setNeutralButton("取消修改", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    // 用户点击取消修改按钮时的处理
                                    dialog.dismiss();
                                }
                            })
                            .setNegativeButton("删除标签",new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    // 用户点击删除标签按钮时的处理
                                    SQLiteDatabase db = new DatabaseHelper(context,context).getWritableDatabase();
                                    SqlController.deleteSmallTag(db,selectedTags,smalltip);
                                    onStart();
                                }
                            });
                    // 创建并显示对话框
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }

            }
        });
    }


    public static void showCustomDialog(Context context) {

    }



}